package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.CarBrandMapper;
import com.weipai.demo.mapper.CarParameterMapper;
import com.weipai.demo.mapper.VehicleModelMapper;
import com.weipai.demo.mapper.base.DCarBrandMapper;
import com.weipai.demo.mapper.base.DCarParameterMapper;
import com.weipai.demo.mapper.base.DVehicleModelMapper;
import com.weipai.demo.model.CarBrand;
import com.weipai.demo.model.CarParameter;
import com.weipai.demo.model.VehicleModel;
import com.weipai.demo.service.CarPriceService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/25 16:17
 * @Version 1.0
 */
@Service
public class CarPriceServiceImpl implements CarPriceService {

    private static final Logger log = LoggerFactory.getLogger(CarPriceServiceImpl.class);

    @Autowired
    private DCarBrandMapper dCarBrandMapper;
    @Autowired
    private DCarParameterMapper dCarParameterMapper;
    @Autowired
    private DVehicleModelMapper dVehicleModelMapper;
    @Autowired
    private VehicleModelMapper vehicleModelMapper;
    @Autowired
    private CarParameterMapper carParameterMapper;
    @Autowired
    private CarBrandMapper carBrandMapper;
    //车贷信息的添加或修改
    @Override
    public Response save(CarBrand carBrand, CarParameter carParameter, VehicleModel vehicleModel) {


        Response response=new Response();
        if (carBrand.getbId()==null){
            Integer bid = (int) System.currentTimeMillis();
            carBrand.setbId(bid);
            vehicleModel.setbId(bid);

            int i=carBrandMapper.selectBybrand(carBrand.getBrand());
            List cid=vehicleModelMapper.selectByvehicleModel(vehicleModel.getVehicleModel());
            if (i>0||cid.size()>0){
                response.setCode(RetCode.IDENTICAL);
                return response;
            }
            int insert1 = dCarBrandMapper.insertSelective(carBrand);
            dVehicleModelMapper.insert(vehicleModel);
            vehicleModelMapper.selectByvehicleModel(vehicleModel.getVehicleModel());

            for (int j = 0; j < cid.size(); j++) {
                carParameter.setcId(j);
            }
            dCarParameterMapper.insert(carParameter);
            response.setCode(RetCode.SUCCESS);
        }else {
            if (carBrand.getbId()!=null&&carParameter.getId()!=null&&vehicleModel.getId()!=null){
                int i = dCarBrandMapper.updateByPrimaryKey(carBrand);
                int j = dCarParameterMapper.updateByPrimaryKey(carParameter);
                int G = dVehicleModelMapper.updateByPrimaryKey(vehicleModel);
            }
        }
        return response;
    }


    //==================================================================================================================


    //前端页面车贷信息显示
    @Override
    public Response frontShow(String display) {
        Response response=new Response();
        try {
            CarBrand carBrand=new CarBrand();
            carBrand.setDisplay(display);
            List<CarBrand> brandList = dCarBrandMapper.select(carBrand);
            response.put("brandList",brandList);
            response.setCode(RetCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            log.error("pageShow"+e.getMessage());
        }
        return response;
    }


    @Override
    public Response frontVehicle(String bId) {
        Response response=new Response();



        return null;
    }


    //==================================================================================================================


    //后台显示车贷信息
    @Override
    public LayuiTablePagingResult pageShow(Integer page, Integer size, String display) {
        LayuiTablePagingResult result= null;
        try {

            CarBrand carBrand=new CarBrand();
            carBrand.setDisplay("1");
            List<CarBrand> brandList = dCarBrandMapper.select(carBrand);



            PageHelper.startPage(page,size);
            //关联c_car_brand,c_car_parameter,c_vehicle_model三张表查询
            List carbarnd = carBrandMapper.selectBydisplay(display);
            //List<JobNews> banners = jobNewsMapper.select(jobNews);

            PageInfo pageInfo=new PageInfo<>(carbarnd);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return result;

    }



}
